package com.cc.service.generate;


import cn.hutool.core.collection.CollectionUtil;
import com.cc.build.BuiderDto;
import com.cc.config.PackagePathConfiguration;
import com.cc.config.TemplateConfiguration;
import com.cc.pojo.bo.TransBo;
import com.cc.pojo.file.BuiderAbstract;
import com.cc.pojo.file.FilePathAndClassName;
import com.cc.pojo.tablestruct.LibaryProperties;
import com.cc.pojo.tablestruct.TableStruct;
import com.cc.service.core.GenerateAbstract;
import com.cc.utils.ConvertHumpUnderlinesToEachOtherUtil;
import com.cc.utils.CoreUtil;
import com.cc.utils.beanUtil.BeanUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author c.c
 * @Description 核心生成类
 * @since 2023/3/30
 */

public class GenerateDto extends GenerateAbstract {
  private PackagePathConfiguration packagePathConfiguration = BeanUtils.getBean(PackagePathConfiguration.class);
  private TemplateConfiguration templateConfiguration = BeanUtils.getBean(TemplateConfiguration.class);

  public GenerateDto() {
    this.getTargetPackageAddress();
  }

  @Override
  public List<FilePathAndClassName<BuiderAbstract>> getTargetPackageAddress(Object... youParameter) {
    List<LibaryProperties> libaryProperties = super.getLibaryProperties();
    List<FilePathAndClassName<BuiderAbstract>> packageAddress = new ArrayList<>();
    libaryProperties.forEach(libaryProperty -> {
      if (CollectionUtil.isNotEmpty(libaryProperty.getTableStructs())) {
        StringBuilder packgePath = new StringBuilder();
        //获取输出路径
        CoreUtil.getPath(libaryProperty, packgePath, packagePathConfiguration.isDtoisLastLayer(), packagePathConfiguration.getDtoPath());
        //获取类名
        String classname = ConvertHumpUnderlinesToEachOtherUtil.underlineToHump(libaryProperty.getTableName(), true);
        String classnameDto = classname + "Dto";
        String classnameDb = classname + "Db";
        List<String> importPath = Collections.singletonList(super.getPathAddress(classnameDb));

        //获取生成类
        List<TransBo> transBos = libaryProperty.getTableStructs()
          .stream()
          .map(TableStruct::convertBuiderDbDtoByTableStruct)
          .collect(Collectors.toList());
        //
        BuiderDto buiderDto = new BuiderDto(packgePath.toString(), importPath, classnameDto, classnameDb, transBos);

        super.putPathAddress(classnameDto, packgePath);
        //转换路径并封装为一个类
        FilePathAndClassName<BuiderAbstract> filePathAndClassName = new FilePathAndClassName<>(packgePath.toString(), classnameDto, buiderDto);
        packageAddress.add(filePathAndClassName);
      }
    });
    super.build(packageAddress, templateConfiguration.getDtoPath());
    return packageAddress;
  }
}
